<?php

namespace Home\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Home\Model\Bangdiem;
use Home\Form\BangdiemForm;
use Zend\Db\Sql\Select;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;
use Zend\Session\Container;

class BangdiemController extends AbstractActionController {

    protected $bangdiemTable;
	public $menuTable;
	public $tintucTable;
	public $onlineTable;
	public $lienketTable;
	public $counterTable;
	public $visitTable;
	public $thongtinTable;

	
	public function counter(){
		$sid = session_id();
		$CurrentTime = time();
		$TimeOut = $CurrentTime - 300;
		$this->getOnlineTable()->check_exists($sid,$CurrentTime,$TimeOut);
		$select = new Select();
		$online = $this->getOnlineTable()->fetchAll()->count();
		$this->getEvent()->getViewModel()->online = $online;
		////
		$today = date('d');
		$thang = date('m');
		$nam = date('Y');
		$ip = $this->getRequest()->getServer('REMOTE_ADDR');
		if($this->getVisitTable()->check_date($today,$thang,$nam))
		{
			if($this->getVisitTable()->check_ip($ip,$today,$thang,$nam))
			{
				if($sid!=$this->getVisitTable()->check_ip($ip,$today,$thang,$nam)->session)
				{
					$this->getVisitTable()->updateVisit($ip,$sid,$today,$thang,$nam);
					$this->getCounterTable()->updateLuotxem($today,$thang,$nam);
				}
			}
			else
			{
				$this->getVisitTable()->insertVisit($ip,$sid,$today,$thang,$nam);
				$this->getCounterTable()->updateLuongtruycap($today,$thang,$nam);
			}
		}
		else
		{
			$this->getVisitTable()->deleteVisit();
			$this->getVisitTable()->insertVisit($ip,$sid,$today,$thang,$nam);
			$this->getCounterTable()->insertCounter($today,$thang,$nam);
		}
		//$this->getCounterTable()->check_exists($today,$thang,$nam);
		
		$select = new Select();
		$tongtruycap = $this->getCounterTable()->sumCounter();
		
		$this->getEvent()->getViewModel()->tongtruycap = $tongtruycap->luongtruycap;
		/////// \Zend\Debug\Debug::dump($tongtruycap->luongtruycap);
		
		$select = new Select();
		$thongtin = $this->getThongtinTable()->fetchAll();
		
		$this->getEvent()->getViewModel()->banner = $thongtin->banner;
		$this->getEvent()->getViewModel()->footer = $thongtin->footer;
		$this->getEvent()->getViewModel()->yahoo = $thongtin->yahoo;
		$this->getEvent()->getViewModel()->skype = $thongtin->skype;
	}
    public function indexAction() {
		$module = "home";
		$controller = $this->params('controller');
		$action = $this->params('action');
		
		$sessionModule = new Container('module');
		$sessionController = new Container('controller');
		$sessionAction = new Container('action');
		$sessionModule->lastModule = $module;
		$sessionController->lastController = $controller;
		$sessionAction->lastAction = $action;
		
		$select = new Select();
		
		//// domain
		$domain = "/truonghoc";
		$this->getEvent()->getViewModel()->domain = $domain;
		///////
		$this->getEvent()->getViewModel()->chuyenmuc = "bangdiem";

        $danhmuc = $this->getMenuTable()->fetchAllDanhmuc();
        $this->getEvent()->getViewModel()->danhmuc = $danhmuc;

		$canbiet = $this->getMenuTable()->fetchAllCanbiet();
        $this->getEvent()->getViewModel()->canbiet = $canbiet;
		
		$menuchinh = $this->getMenuTable()->fetchAllMenuchinh();
        $this->getEvent()->getViewModel()->menuchinh = $menuchinh;
		$this->getEvent()->getViewModel()->menuchinh1 = $menuchinh;
		
		$select = new Select();
		$noibat = $this->getTintucTable()->fetchAll($select->where(array('tinnoibat' => 'Nổi bật','trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
		$this->getEvent()->getViewModel()->noibat = $noibat;
		
		/////
		$select = new Select();
		$moinhat = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
		$this->getEvent()->getViewModel()->moinhat = $moinhat;
		
		/////
		/////
		$select = new Select();
		$quantam = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('soluotxem desc')->limit(5));
		$this->getEvent()->getViewModel()->quantam = $quantam;
		
		/////
		/////
		$select = new Select();
		$lienketwebsite = $this->getLienketTable()->fetchAll();
		$this->getEvent()->getViewModel()->lienketwebsite = $lienketwebsite;
		////
		$this->counter();
		
		
		$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $form = new BangdiemForm($dbAdapter);
        $form->get('submit')->setAttribute('value', 'Xem điểm');
		
		

       	
        $request = $this->getRequest();
        if ($request->isPost()) {
			$select = new Select();
        	$bangdiems_m = $this->getBangdiemTable()->fetchAll($select->where('namhoc = "'.$_POST['namhoc'].'" AND loaidiem = 1 AND hocki = "'.$_POST['hocki'].'" AND (mahs = "'.$_POST['mahs'].'" OR tenhs = "'.$_POST['mahs'].'")')->order('tenmonhoc ASC'));
			//
			$select = new Select();
        	$bangdiems_15p = $this->getBangdiemTable()->fetchAll($select->where('namhoc = "'.$_POST['namhoc'].'" AND loaidiem = 2 AND hocki = "'.$_POST['hocki'].'" AND (mahs = "'.$_POST['mahs'].'" OR tenhs = "'.$_POST['mahs'].'")')->order('tenmonhoc ASC'));
			//
			$select = new Select();
        	$bangdiems_1t = $this->getBangdiemTable()->fetchAll($select->where('namhoc = "'.$_POST['namhoc'].'" AND loaidiem = 3 AND hocki = "'.$_POST['hocki'].'" AND (mahs = "'.$_POST['mahs'].'" OR tenhs = "'.$_POST['mahs'].'")')->order('tenmonhoc ASC'));
			//
			$select = new Select();
        	$bangdiems_kt = $this->getBangdiemTable()->fetchAll($select->where('namhoc = "'.$_POST['namhoc'].'" AND loaidiem = 4 AND hocki = "'.$_POST['hocki'].'" AND (mahs = "'.$_POST['mahs'].'" OR tenhs = "'.$_POST['mahs'].'")')->order('tenmonhoc ASC'));
			//
			$select = new Select();
        	$bangdiems_tb = $this->getBangdiemTable()->fetchAll($select->where('namhoc = "'.$_POST['namhoc'].'" AND loaidiem = 5 AND hocki = "'.$_POST['hocki'].'" AND (mahs = "'.$_POST['mahs'].'" OR tenhs = "'.$_POST['mahs'].'")')->order('tenmonhoc ASC'));
			
			$select = new Select();
			$thongtin = $this->getBangdiemTable()->fetchAll($select->where('mahs = "'.$_POST['mahs'].'" OR tenhs = "'.$_POST['mahs'].'"')->limit(1));
			$hocki = $request->getPost('hocki');
			$namhoc = $request->getPost('namhoc');
			return new ViewModel(array(
					'bangdiem_m' => $bangdiems_m,
					'bangdiem_15p' => $bangdiems_15p,
					'bangdiem_1t' => $bangdiems_1t,
					'bangdiem_kt' => $bangdiems_kt,
					'bangdiem_tb' => $bangdiems_tb,
					'form' => $form,
					'thongtin' => $thongtin,
					'hocki' => $hocki,
					'namhoc' => $namhoc,
                ));
		
		}
		return array('form' => $form);
    }

    

    public function getBangdiemTable() {
        if (!$this->bangdiemTable) {
            $sm = $this->getServiceLocator();
            $this->bangdiemTable = $sm->get('Home\Model\BangdiemTable');
        }
        return $this->bangdiemTable;
    }
	public function getMenuTable() {
        if (!$this->menuTable) {
            $sm = $this->getServiceLocator();
            $this->menuTable = $sm->get('Home\Model\MenuTable');
        }
        return $this->menuTable;
    }
	public function getTintucTable() {
        if (!$this->tintucTable) {
            $sm = $this->getServiceLocator();
            $this->tintucTable = $sm->get('Home\Model\TintucTable');
        }
        return $this->tintucTable;
    }
	public function getOnlineTable() {
        if (!$this->onlineTable) {
            $sm = $this->getServiceLocator();
            $this->onlineTable = $sm->get('Home\Model\OnlineTable');
        }
        return $this->onlineTable;
    }
	public function getLienketTable() {
        if (!$this->lienketTable) {
            $sm = $this->getServiceLocator();
            $this->lienketTable = $sm->get('Home\Model\LienketTable');
        }
        return $this->lienketTable;
    }
	public function getCounterTable() {
        if (!$this->counterTable) {
            $sm = $this->getServiceLocator();
            $this->counterTable = $sm->get('Home\Model\CounterTable');
        }
        return $this->counterTable;
    }
	public function getVisitTable() {
        if (!$this->visitTable) {
            $sm = $this->getServiceLocator();
            $this->visitTable = $sm->get('Home\Model\VisitTable');
        }
        return $this->visitTable;
    }
	public function getThongtinTable() {
        if (!$this->thongtinTable) {
            $sm = $this->getServiceLocator();
            $this->thongtinTable = $sm->get('Home\Model\ThongtinTable');
        }
        return $this->thongtinTable;
    }


}
